/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 *\author: starlove
 *************************************************************************/
#ifndef __CC_PYTHON_IMPORT_O_FILEUTILS_IMPL__
#define __CC_PYTHON_IMPORT_O_FILEUTILS_IMPL__

#include "python_object_fileutils.h"

NS_CCPY_BEGIN
	PYCLASS_INIT_F(CPyFileUtils, "sharedFileUtils", "Python sharedFileUtils Object");
	PYCLASS_BEGIN_METHOD_DEF(CPyFileUtils)
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, purgeCachedEntries			, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, getFileData					, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, getFileDataFromZip			, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, fullPathForFilename			, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, fullPathFromRelativeFile	, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, setSearchResolutionsOrder	, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, addSearchResolutionsOrder	, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, getSearchResolutionsOrder	, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, setSearchPaths				, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, addSearchPath				, METH_STATIC|METH_VARARGS)    
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, getSearchPaths				, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, getWritablePath				, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, isFileExist					, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, isAbsolutePath				, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, setPopupNotify				, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyFileUtils, isPopupNotify				, METH_STATIC|METH_VARARGS)
	PYCLASS_END_METHOD_DEF(CPyFileUtils)
	PYCLASS_BEGIN_GETSET_DEF(CPyFileUtils)
	PYCLASS_END_GETSET_DEF(CPyFileUtils)
	PYCLASS_IMP_INTERFACE_NEW(CPyFileUtils)


	int CPyFileUtils::__init__(CPyFileUtils *pySelf, PyObject *args, PyObject *kwds)
	{
		return 0;
	}

	void CPyFileUtils::__del__(CPyFileUtils *self)
	{
		self->ob_type->tp_free(self);
	}
	
	PYCLASS_METHOD_IMPL(CPyFileUtils, purgeCachedEntries)
	{
		cocos2d::CCFileUtils::sharedFileUtils()->purgeCachedEntries();
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyFileUtils, getFileData)
	{
		char *filename = NULL;
		char *filemode = NULL;
		if( !PyArg_ParseTuple(pyArgs, "ss", &filename, &filemode) )
			Py_RETURN_NONE;
		unsigned long l = 0;
		unsigned char *data = cocos2d::CCFileUtils::sharedFileUtils()->getFileData(filename, filemode, &l);
		return Py_BuildValue("s#", data, l);
	}

	PYCLASS_METHOD_IMPL(CPyFileUtils, getFileDataFromZip)
	{
		char *filename = NULL;
		char *zipfile = NULL;
		if( !PyArg_ParseTuple(pyArgs, "ss", &zipfile, &filename) )
			Py_RETURN_NONE;
		unsigned long l = 0;
		unsigned char *data = cocos2d::CCFileUtils::sharedFileUtils()->getFileDataFromZip(zipfile, filename, &l);
		return Py_BuildValue("s#", data, l);
	}

	PYCLASS_METHOD_IMPL(CPyFileUtils, fullPathForFilename)
	{
		char *filename = NULL;
		if( !PyArg_ParseTuple(pyArgs, "s", &filename) )
			Py_RETURN_NONE;
		std::string strPath = cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(filename);
		return Py_BuildValue("s", strPath.c_str());
	}

	PYCLASS_METHOD_IMPL(CPyFileUtils, fullPathFromRelativeFile)
	{
		char *filename = NULL;
		char *relative = NULL;
		if( !PyArg_ParseTuple(pyArgs, "ss", &filename, &relative) )
			Py_RETURN_NONE;
		const char *strPath = cocos2d::CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile(filename, relative);
		return Py_BuildValue("s", strPath);
	}

	PYCLASS_METHOD_IMPL(CPyFileUtils, setSearchResolutionsOrder)
	{
		PyObject *py = NULL;
		if( !PyArg_ParseTuple(pyArgs, "O", &py) )
			Py_RETURN_NONE;
		if(!py || !PySequence_Check(py))
			Py_RETURN_NONE;
		Py_ssize_t s = PySequence_Size(py);
		std::vector<std::string> paths;
		for(Py_ssize_t i = 0; i < s; i ++)
		{
			paths.push_back(std::string(PyString_AsString(PySequence_GetItem(py, i))));
		}

		cocos2d::CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(paths);
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyFileUtils, addSearchResolutionsOrder)
	{
		char *order = NULL;
		if( !PyArg_ParseTuple(pyArgs, "s", &order) )
			Py_RETURN_NONE;

		cocos2d::CCFileUtils::sharedFileUtils()->addSearchResolutionsOrder(order);
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyFileUtils, getSearchResolutionsOrder)
	{
		std::vector<std::string> paths = cocos2d::CCFileUtils::sharedFileUtils()->getSearchResolutionsOrder();
		PyObject *pyList = PyList_New(paths.size());
		for(size_t i = 0; i < paths.size(); i ++)
		{
			PyList_Append(pyList, Py_BuildValue("s", paths[i].c_str()));
		}
		return PyList_AsTuple(pyList);
	}

	PYCLASS_METHOD_IMPL(CPyFileUtils, setSearchPaths)
	{
		PyObject *py = NULL;
		if( !PyArg_ParseTuple(pyArgs, "O", &py) )
			Py_RETURN_NONE;
		if(!py || !PySequence_Check(py))
			Py_RETURN_NONE;
		Py_ssize_t s = PySequence_Size(py);
		std::vector<std::string> paths;
		for(Py_ssize_t i = 0; i < s; i ++)
		{
			paths.push_back(std::string(PyString_AsString(PySequence_GetItem(py, i))));
		}

		cocos2d::CCFileUtils::sharedFileUtils()->setSearchPaths(paths);
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyFileUtils, addSearchPath)
	{
		char *order = NULL;
		if( !PyArg_ParseTuple(pyArgs, "s", &order) )
			Py_RETURN_NONE;

		cocos2d::CCFileUtils::sharedFileUtils()->addSearchPath(order);
		Py_RETURN_NONE;
	}
 
	PYCLASS_METHOD_IMPL(CPyFileUtils, getSearchPaths)
	{
		std::vector<std::string> paths = cocos2d::CCFileUtils::sharedFileUtils()->getSearchPaths();
		PyObject *pyList = PyList_New(paths.size());
		for(size_t i = 0; i < paths.size(); i ++)
		{
			PyList_Append(pyList, Py_BuildValue("s", paths[i].c_str()));
		}
		return PyList_AsTuple(pyList);
	}

	PYCLASS_METHOD_IMPL(CPyFileUtils, getWritablePath)
	{
		std::string strPath = cocos2d::CCFileUtils::sharedFileUtils()->getWritablePath();
		return Py_BuildValue("s", strPath.c_str());
	}

	PYCLASS_METHOD_IMPL(CPyFileUtils, isFileExist)
	{
		char *f = NULL;
		if( !PyArg_ParseTuple(pyArgs, "s", &f) )
			Py_RETURN_NONE;
		std::string str = f;
		bool v = cocos2d::CCFileUtils::sharedFileUtils()->isFileExist(str);
		Py_RETURN_BOOL(v);
	}

	PYCLASS_METHOD_IMPL(CPyFileUtils, isAbsolutePath)
	{
		char *f = NULL;
		if( !PyArg_ParseTuple(pyArgs, "s", &f) )
			Py_RETURN_NONE;
		std::string str = f;
		bool v = cocos2d::CCFileUtils::sharedFileUtils()->isAbsolutePath(str);
		Py_RETURN_BOOL(v);
	}

	PYCLASS_METHOD_IMPL(CPyFileUtils, setPopupNotify)
	{
		PyObject *f = NULL;
		if( !PyArg_ParseTuple(pyArgs, "O", &f) )
			Py_RETURN_NONE;
		cocos2d::CCFileUtils::sharedFileUtils()->setPopupNotify(f == Py_True);
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyFileUtils, isPopupNotify)
	{
		bool v = cocos2d::CCFileUtils::sharedFileUtils()->isPopupNotify();
		Py_RETURN_BOOL(v);
	}

NS_CCPY_END

#endif